﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Service.DI
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AutoRegisterServices(this IServiceCollection services)
        {

            // 注册service层的程序集 程序集名称（不带.dll）
            var serviceAssembly = Assembly.Load("Service");
            AutoRegisterByNamingConvention(services, serviceAssembly, typeof(ITransientService));
            AutoRegisterByNamingConvention(services, serviceAssembly, typeof(IScopedService));
            AutoRegisterByNamingConvention(services, serviceAssembly, typeof(ISingletonService));


            // 注册ViewModel层的程序集 程序集名称（不带.dll）
            var viewModelAssembly = Assembly.Load("ViewModel");
            AutoRegisterByNamingConvention(services, viewModelAssembly, typeof(ITransientService));
            AutoRegisterByNamingConvention(services, viewModelAssembly, typeof(IScopedService));
            AutoRegisterByNamingConvention(services, viewModelAssembly, typeof(ISingletonService));

            // 获取当前应用程序域中的所有程序集
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (var assembly in assemblies)
            {
                AutoRegisterByNamingConvention(services, assembly, typeof(ITransientService));
                AutoRegisterByNamingConvention(services, assembly, typeof(IScopedService));
                AutoRegisterByNamingConvention(services, assembly, typeof(ISingletonService));
            }
            return services;
        }

        private static void AutoRegisterByNamingConvention(IServiceCollection services, Assembly assembly,
                Type type)
        {
            var types = assembly.GetTypes()
                .Where(t => t.IsClass && !t.IsAbstract && type.IsAssignableFrom(t))
                .ToList();
            //var allTypes = assembly.GetTypes().Where(t => t.IsClass && !t.IsAbstract).ToList();

            foreach (var implementationType in types)
            {
                // 查找对应的接口（例如：IUserService 对应 UserService）
                var interfaceName = $"I{implementationType.Name}";
                var interfaceType = implementationType.GetInterfaces()
                    .FirstOrDefault(i => i.Name == interfaceName);
                var lifetime = type.Name;

                if (interfaceType != null)
                {
                    // 根据生命周期注册
                    switch (lifetime)
                    {
                        case nameof(ISingletonService):
                            services.AddSingleton(interfaceType, implementationType);
                            break;
                        case nameof(IScopedService):
                            services.AddScoped(interfaceType, implementationType);
                            break;
                        case nameof(ITransientService):
                            services.AddTransient(interfaceType, implementationType);
                            break;
                    }

                    //Console.WriteLine($"自动注册: {interfaceType.Name} -> {implementationType.Name} ({lifetime})");
                }
                else
                {
                    // 如果没有对应接口，直接注册实现类
                    switch (lifetime)
                    {
                        case nameof(ISingletonService):
                            services.AddSingleton(implementationType);
                            break;
                        case nameof(IScopedService):
                            services.AddScoped(implementationType);
                            break;
                        case nameof(ITransientService):
                            services.AddTransient(implementationType);
                            break;
                    }

                    //Console.WriteLine($"自动注册: {implementationType.Name} ({lifetime})");
                }
            }

        }
    }
}
